import requests
from bs4 import BeautifulSoup
import csv
def get_html(url):
    head={'User-Agent':'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/122.0.0.0 Safari/537.36'}
    try:
        r=requests.get(url=url)
        r.encoding=r.apparent_encoding
        r.raise_for_status()
        return r.text
    except Exception as e:
        print(e)
def parser(html):
    soup=BeautifulSoup(html,'lxml')
    out_list=[]
    for row in soup.select('#pane-news ul>li'):
        row_data=[row.text.replace('','').replace('\n',''),
                  row.select('a')[0].attrs['href'].replace('','').replace('\n','')]
        out_list.append(row_data)
    return out_list
def save_csv(path,shuju):
    with open(path,'a+',newline="",encoding='utf-8') as f:
        csv_write=csv.writer(f)
        csv_write.writerows(shuju)


if __name__ == '__main__':
    url="http://www.bspider.top/baidunews/"
    yuanma=get_html(url)
    out_list=parser(yuanma)
    path='d:\\fd.csv'
    save_csv(path,out_list)